#include<iostream>
#include<vector>
#include<string>

using namespace std;

class Solution {
public:
    vector<string> split(string str, string pattern)
    {
        string::size_type pos;
        vector<string> result;
        str += pattern;
        int size = str.size();
        for (int i = 0; i < size; i++)

        {
            pos = str.find(pattern, i);
            if (pos < size)
            {
                string s = str.substr(i, pos - i);
                result.push_back(s);
                i = pos + pattern.size() - 1;
            }
        }
        return result;
    }

    string reverseString(string s) {
        int left = 0, right = s.size() - 1;
        while (left < right) {
            swap(s[left], s[right]);
            left++;
            right--;
        }
        return s;
    }

    string reverseWords(string s) {
        vector<string> collect=split(s," ");
        string result = "";
        for (int i = 0; i < collect.size(); i++) {
            string reverse=reverseString(collect[i]);
            result += reverse;
            if(i!=collect.size()-1) result += " ";
        }
        return result;
    }

    string reverseWordsDP(string s) {
        int length = s.length();
        int i = 0;
        while (i < length) {
            int start = i;
            while (i < length && s[i] != ' ') {
                i++;
            }

            int left = start, right = i - 1;
            while (left < right) {
                swap(s[left], s[right]);
                left++;
                right--;
            }
            while (i < length && s[i] == ' ') {
                i++;
            }
        }
        return s;
    }


};

int main() {
    Solution s;
    string str = "God Ding";
    string res=s.reverseWords(str);
    cout << res << endl;
}

